//
//  JiaZDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JiaZDetViewController.h"

#import "GXDPView.h"
#import "PinJiaCell.h"
#import "JiaZhenCell.h"
#import "PingJiaViewController.h"
#import "KuLiViewController.h"
@interface JiaZDetViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSMutableArray *datas;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation JiaZDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"店铺详情"];
    array = [NSMutableArray array];
    datas = [NSMutableArray array];
    
    [self loadData];
    [self loadData2];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-60) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    //头视图
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 290);
    _tableView.tableHeaderView = view;
    
    view.labName.text = _name;
    view.labdet.text = _det;
    view.labPlace.text = [NSString stringWithFormat:@"地址：%@",_place];
    view.labOrder.text = _order;
    
    
    UILabel *labs = [[UILabel alloc] initWithFrame:CGRectMake(0, 280, ScreenWidth, 10)];
    labs.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:labs];
    
    //尾
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60)];
    footView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:footView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 10, ScreenWidth/3, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"呼叫苦力" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTEL) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    UIView *views = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth/3*2, 50)];
    views.backgroundColor = [UIColor whiteColor];
    [footView addSubview:views];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = _name;
    lab1.font = [UIFont systemFontOfSize:13.0];
    [views addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(views.top).offset(8);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.text = _place;
    lab2.textColor = [UIColor darkGrayColor];
    lab2.font = [UIFont systemFontOfSize:11.0];
    [views addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(lab1.bottom).offset(8);
    }];
    
}
//呼叫苦力
- (void)clickTEL{
    KuLiViewController *kuli = [[KuLiViewController alloc] init];
    [self.navigationController pushViewController:kuli animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return array.count;
    }
    return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *cellId = @"Cell1";
        JiaZhenCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"JiaZhenCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = array[indexPath.row];
        cell.labName.text = [dic objectForKey:@"name"];
        cell.labDet.text = [dic objectForKey:@"synopsis"];
        cell.labPrice.text = [NSString stringWithFormat:@"￥%.2f元/小时",[[dic objectForKey:@"price"] floatValue]];
        
        return cell;
    }else{
        static NSString *cellId = @"Cell2";
        PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = datas[indexPath.row];
        cell.labName.text = [dic objectForKey:@"nickname"];
        cell.labDate.text = [dic objectForKey:@"commenttime"];
        cell.labText.text = [dic objectForKey:@"comment"];
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60.0;
    }
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *labs = [[UILabel alloc] initWithFrame:CGRectMake(16, 12, 4, 16)];
    labs.backgroundColor = [UIColor blueColor];
    [view addSubview:labs];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 100, 20)];
    lab.text = @"店铺评价";
    lab.textColor = COLOR_RGB(77, 77, 77, 1);
    lab.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];
    
    UILabel *labpl = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-100-16, 10, 100, 20)];
    NSInteger indexs = datas.count;
    labpl.text = [NSString stringWithFormat:@"总评价：%li",indexs];
    labpl.font = [UIFont systemFontOfSize:14.0];
    labpl.textColor = [UIColor darkGrayColor];
    labpl.textAlignment = NSTextAlignmentRight;
    [view addSubview:labpl];
    if (section == 0) {
        labpl.hidden = YES;
        lab.text = @"精选服务";
    }
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10.0)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        
        return view;
    }else{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
        view.backgroundColor = [UIColor whiteColor];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
        [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
        NSInteger indexs = datas.count;
        lab.text = [NSString stringWithFormat:@" (%li)",indexs];
        lab.font = [UIFont systemFontOfSize:12.0];
        lab.textColor = [UIColor redColor];
        [view addSubview:lab];
        
        return view;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 10.0;
    }
    return 40.0;
}

- (void)click{
    PingJiaViewController *pingjia = [[PingJiaViewController alloc] init];
    [self.navigationController pushViewController:pingjia animated:YES];
}
//家政详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/houservice",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政详情res:%@",responseObject);
        array = [[responseObject objectForKey:@"result"] objectForKey:@"service"];
        datas = [[[responseObject objectForKey:@"result"] objectForKey:@"comment"] objectForKey:@"comment"];
        [_tableView reloadData];
        [self loadUI];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        
        [SVProgressHUD dismiss];
    }];
    
}
//评论
- (void)loadData2{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:@"1" forKey:@"p"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/comment",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"评论res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        //
        if ([error integerValue] == 200) {
            datas = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
